import { categoryList, reqCategory1List, reqCategory2List, reqCategory3List } from '@/api/Product/category'
import { createSlice } from '@reduxjs/toolkit'
import { AppDispatch, RootState } from './store'

interface categoryItemType {
    category1id: number | undefined;
    category2id: number | undefined;
    category3id: number | undefined;
    category1List: categoryList
    category2List: categoryList
    category3List: categoryList
}
let initialState: categoryItemType = {
    category1id: undefined,
    category2id: undefined,
    category3id: undefined,
    category1List: [],
    category2List: [],
    category3List: [],
}
const category = createSlice({
    name: "category",
    initialState,
    reducers: {
        setCategory1id: (state, { payload }) => {
            state.category1id = payload
        },
        setCategory2id: (state, { payload }) => {
            state.category2id = payload
        },
        setCategory3id: (state, { payload }) => {
            state.category3id = payload
        }
        ,
        setCategory1List: (state, { payload }) => {
            state.category1List = payload
        },
        setCategory2List: (state, { payload }) => {
            state.category2List = payload
        },
        setCategory3List: (state, { payload }) => {
            state.category3List = payload
        },
    },

})
// 异步函数
export function getCategory1List() {
    return async (dispath: AppDispatch) => {
        let re = await reqCategory1List()
        dispath(setCategory1List(re))
    }
}
export function getCategory2List(payload: number) {
    return async (dispath: AppDispatch) => {
        let re = await reqCategory2List(payload)
        dispath(setCategory1id(payload))
        dispath(setCategory2List(re))
    }
}
export function getCategory3List(payload: number) {
    return async (dispath: AppDispatch) => {
        let re = await reqCategory3List(payload)
        dispath(setCategory2id(payload))
        dispath(setCategory3List(re))
    }
}

export default category.reducer
export const { setCategory1List, setCategory2List, setCategory3List, setCategory1id, setCategory2id, setCategory3id } = category.actions
export const category1id = (store: RootState) => store.category.category1id
export const category1List = (store: RootState) => store.category.category1List
export const category2List = (store: RootState) => store.category.category2List
export const category3List = (store: RootState) => store.category.category3List
export const category2id = (store: RootState) => store.category.category2id
export const category3id = (store: RootState) => store.category.category3id